package com.gdust.hzx.manager.controller;

import com.gdust.hzx.manager.service.SysUserService;
import com.gdust.spzx.common.log.annotation.Log;
import com.gdust.spzx.common.log.enums.OperatorType;
import com.gdust.hzx.model.dto.system.AssginRoleDto;
import com.gdust.hzx.model.dto.system.SysUserDto;
import com.gdust.hzx.model.entity.system.SysUser;
import com.gdust.hzx.model.vo.common.Result;
import com.gdust.hzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: SysUserController
 * Package: com.gdust.spzx.manager.controller
 * Description:
 *
 * @Author syun
 * @Create 2024/11/22 17:21
 * @Version 1.0
 */
@RestController// @RestController: 专用于restful风格的注解 = @Controller (控制器) + @ResponseBody (方法返回值为json)
@RequestMapping(value = "/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户条件分页查询接口
     *
     * 注：动态筛选条件：根据用户传入的查询条件（如keyword、createTimeBegin、createTimeEnd）进行筛选，<if>标签根据条件的值是否存在来决定是否添加相应的 WHERE子句
     *
     * @param pageNum 当前页
     * @param pageSize 每页记录数
     * @param sysUserDto 请求参数实体类，内含搜索关键字、开始时间和结束时间等信息。
     * @return 响应结果实体类返回结果与业务状态码
     */
//    @Log(title = "用户分页",businessType = 0,operatorType = OperatorType.MANAGE)
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable("pageNum") Integer pageNum,
                             @PathVariable("pageSize") Integer pageSize,
                             SysUserDto sysUserDto){
        //自定义分页查询方法
        PageInfo<SysUser> pageInfo= sysUserService.findByPage(pageNum,pageSize,sysUserDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加用户接口
     * @param sysUser 系统用户实体类
     * @return 响应结果实体类返回结果与业务状态码
     */
    @Log(title = "添加用户",businessType = 1,operatorType = OperatorType.MANAGE)
    @PostMapping(value = "/saveSysUser")
    public Result saveSysUser(@RequestBody SysUser sysUser){
        //自定义方法添加用户接口
        sysUserService.saveSysUser(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户修改
     * @param sysUser 系统用户实体类
     * @return 响应结果实体类返回结果与业务状态码
     */
    @Log(title = "修改用户",businessType = 2,operatorType = OperatorType.MANAGE)
    @PutMapping(value = "/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser){
        //自定义方法修改用户接口
        sysUserService.updateSysUser(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户删除
     * @param userId 用户id
     * @return 响应结果实体类返回结果与业务状态码
     */
    @Log(title = "删除用户",businessType = 3,operatorType = OperatorType.MANAGE)
    @DeleteMapping(value = "/deleteById/{userId}")
    public Result deleteById(@PathVariable(value = "userId") Long userId){
        sysUserService.deleteById(userId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户分配角色 保存分配数据
     * @param assginRoleDto 请求参数实体类
     * @return 响应结果实体类返回结果与业务状态码
     */
    @Log(title = "分配用户角色",businessType = 0,operatorType = OperatorType.MANAGE)
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto){
        //自定义方法：用户分配角色 保存分配数据
        sysUserService.doAssign(assginRoleDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
